<template>
  <div class="view-container notice-list">

    <header class="view-header">
      <div class="header-container">
        <div class="header-title">
          <i class="el-icon-s-grid"></i>
          <span>通知公告</span>
        </div>
        <div class="header-form">
          <div class="header-form-item">
            <el-button plain type="danger" icon="el-icon-close" :loading="loading.undoLoading"  @click="undoPublish">取消发布</el-button>
          </div>
          <div class="header-form-item">
            <el-button plain type="success" icon="el-icon-check" :loading="loading.pubLoading"  @click="publish">发布</el-button>
          </div>
          <div class="header-form-item">
            <el-button plain type="danger" :loading="delLoading" icon="el-icon-delete" @click="del">{{isBatchesDel}}</el-button>
          </div>
          <div class="header-form-item">
            <el-button plain type="primary" icon="el-icon-edit" @click="edit">编辑</el-button>
          </div>
          <div class="header-form-item">
            <el-button plain type="success" icon="el-icon-plus" @click="toAddPage">添加</el-button>
          </div>
          <div class="header-form-item">
            <el-button plain icon="el-icon-search"  @click="fetchData">查询</el-button>
          </div>
          <div class="header-form-item title-search">
            <el-select v-model="searchForm.department" placeholder="所在部门" @change="fetchData" clearable >
              <el-option
                v-for="dept in departmentList"
                :key="dept.value"
                :label="dept.label"
                :value="dept.value"
              />
            </el-select>
          </div>
          <div class="header-form-item title-search">
            <el-input v-model.trim="searchForm.noticeTitle" placeholder="标题" @input="AntiShake" clearable></el-input>
          </div>
        </div>
      </div>
    </header>

    <section class="table-container view-section">
      <el-table
        ref="table"
        :data="tableData"
        stripe
        border
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column type="selection" align="center"/>
        <el-table-column label="标题" >
          <template slot-scope="scope">
            <el-link @click.stop="showDetail(scope.row)" v-text="scope.row.noticeTitle"></el-link>
            <el-tag type="danger" class="un-read-tag" size="mini" v-if="!scope.row.read">未读</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="noticeType" label="分类" width="70" :formatter="noticeTypeFmt" align="center" >
        </el-table-column>
        <el-table-column prop="auditedText" label="状态" width="80" align="center" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.auditedText === '未发布'">未发布</el-tag>
            <el-tag type="success" v-else>已发布</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="添加人" width="80" align="center" />
        <el-table-column prop="departmentName" label="所在部门" align="center" width="100" />
        <el-table-column prop="startTime" label="生效日期" :formatter="startTimeFmt" align="center" width="150" sortable/>
        <el-table-column prop="endTime" label="终止日期" :formatter="endTimeFmt" align="center" width="150" sortable/>
        <el-table-column prop="created" label="添加时间" :formatter="dateFmt" align="center" width="150" sortable/>
      </el-table>
    </section>
    <footer>
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        style="text-align: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        :page-size="page.size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </footer>


  </div>
</template>

<script>
  import departmentAPI from '@/api/system/department'
  import noticeAPI from '@/api/portal/notice'
  import listMixin from '@/mixin/listMixin'

  export default {
    name: 'Notice',
    mixins:[listMixin],
    data() {
      return {
        searchForm:{
          department:null,
          noticeTitle:''
        },
        departmentList: [],
        mixinParams:{
          addUrl:'/portal/add-notice',
          editUrl:'/portal/edit-notice/',
          propertyName:'noticeId',
          API:noticeAPI
        },
        loading:{
          pubLoading:false,
          undoLoading:false
        }
      }
    },
    computed:{
    },
    created() {
      this.getDepartmentList()
    },
    methods: {
      dateFmt(row){
        return new Date(row.created).Format("yyyy-MM-dd hh:mm:ss")
      },
      showDetail(row){
        this.$router.push('/portal/notice-detail/'+row.noticeId)
      },
      getDepartmentList(){
        departmentAPI.getSelectList().then(res=>{
          this.departmentList = res.result
        }).catch(err=>{
          err.LogicalExpression()
        })
      },
      fetchData() {
        if(this.searchForm.department === ''){
          this.searchForm.department = null
        }
        noticeAPI.getList(this.searchForm.department,this.searchForm.noticeTitle, this.page.index,this.page.size).then(res => {
          this.page.total = res.total
          this.tableData = res.records
        }).catch(err => {
          console.log(err)
        })
      },
      noticeTypeFmt(row) {
        return row.noticeType === 1 ? '通知' : '公告'
      },
      startTimeFmt(row){
        return this._util.format(row.startTime)
      },
      endTimeFmt(row){
        return this._util.format(row.endTime)
      },
      publish(){
        if(this.multipleSelected.length === 0){
          this.$message.info('请选择要发布的通知')
          return
        }
        if(this.loading.pubLoading){
          return
        }
        this.loading.pubLoading = true
        noticeAPI.publish(this.multipleSelected).then(res=>{
          this.loading.pubLoading = false
          this.$message.success('发布成功')
          this.fetchData()
        })
      },
      undoPublish(){
        if(this.multipleSelected.length === 0){
          this.$message.info('请选择要取消发布的通知')
          return
        }
        if(this.loading.undoLoading){
          return
        }
        this.loading.undoLoading = true
        noticeAPI.undoPublish(this.multipleSelected).then(res=>{
          this.loading.undoLoading = false
          this.$message.success('取消发布成功')
          this.fetchData()
        })
      }

    }
  }
</script>

<style lang="scss">

  .notice-list {
    .un-read-tag {
      margin-left: 10px;
    }
    .el-link {
      color:#1890ff;
    }
  }

</style>

<style scoped lang="scss">

  .view-header {
    .left-wrap {
      float: left;
    }
    .right-wrap {
      float: right;
    }
  }

</style>
